package org.example.demo14_observe;

import java.util.ArrayList;
import java.util.List;

public class Sensor implements Subject{
    private List<ResponseDevice> observers = new ArrayList<>();
    private int temperature;

    public void setTemperature(int temperature) {
        this.temperature = temperature;
        notifys();
    }
    @Override
    public void attach(ResponseDevice responseDevice) {
        observers.add(responseDevice);
    }

    @Override
    public void detach(ResponseDevice responseDevice) {
        observers.remove(responseDevice);
    }

    @Override
    public void notifys() {
        for (ResponseDevice observer : observers) {
            observer.update(temperature);
        } // 调用观察者的响应方法
    }
}
